//
//  EditViewController.m
//  F&B BallScorer
//
//  Created by dev1 on 2019/6/13.
//  Copyright © 2019 dev1. All rights reserved.
//

#import "EditViewController.h"

@interface EditViewController ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UITextField *durationField;
@property (weak, nonatomic) IBOutlet UITextField *teamAField;
@property (weak, nonatomic) IBOutlet UITextField *teamBField;


@property (weak, nonatomic) IBOutlet UIView *pickerView;
@property (weak, nonatomic) IBOutlet UIPickerView *timePicker;
@property (nonatomic, assign) NSInteger totalDuration;


@end

@implementation EditViewController
{
    NSInteger _hour;
    NSInteger _min;
    NSInteger _sec;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _hour = 0; _min = 0; _sec = 0;
}
- (IBAction)didClickDurationField:(id)sender {
    self.pickerView.hidden = NO;
}
- (IBAction)didClickPickerviewClose:(id)sender {
    self.pickerView.hidden = YES;
}
- (IBAction)didClickDismiss:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)didClickGo:(id)sender {
    teamItem *item = [teamItem new];
    item.Aname = [self kk_isNullString:self.teamAField.text]?@"A":self.teamAField.text;
    item.Bname = [self kk_isNullString:self.teamBField.text]?@"B":self.teamBField.text;
    item.duration = _totalDuration;
    
    UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ScoreViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"ScoreViewController"];
    vc.ball = _ball;
    vc.items = item;
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (BOOL)kk_isNullString:(NSString *)string {
    if (string == nil ||
        string.length == 0 ||
        [string isEqualToString:@"NULL"] ||
        [string isEqualToString:@"Null"] ||
        [string isEqualToString:@"null"] ||
        [string isEqualToString:@"(NULL)"] ||
        [string isEqualToString:@"(Null)"] ||
        [string isEqualToString:@"(null)"] ||
        [string isEqualToString:@"<NULL>"] ||
        [string isEqualToString:@"<Null>"] ||
        [string isEqualToString:@"<null>"] ||
        [string isKindOfClass:[NSNull class]] ||
        string == NULL) {
        return YES;
    }
    return NO;
}




#pragma mark -


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 60;
}


- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    if (component == 0) {
        return [NSString stringWithFormat:@"%ld hour",(long)row];
    }
    if (component == 1) {
        return [NSString stringWithFormat:@"%ld min",(long)row];
    }
    if (component == 2) {
        return [NSString stringWithFormat:@"%ld sec",(long)row];
    }
    return @"0";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    

    if (component == 0) {
        _hour = row;
    }
    if (component == 1) {
        _min = row;
    }
    if (component == 2) {
        _sec = row;
    }
    self.durationField.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld",(long)_hour, (long)_min, (long)_sec];
    
    
    _totalDuration = _hour*3600+_min*60+_sec;
    
}

//点击输入框界面跟随键盘上移

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    
    [UIView setAnimationDuration:0.5f];
    
    self.view.frame = CGRectMake(0.0f, -80, self.view.frame.size.width, self.view.frame.size.height);
    
    [UIView commitAnimations];
    
}



//输入框编辑完成以后，将视图恢复到原始状态

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.view endEditing:YES];
    return YES;
}



@end
